
// 使用接口，泛型等模拟数据库操作
export module SimulateDB {
    interface DBI<T> {
        add(info: T): boolean;
        update(info: T, id: number): boolean;
        delete(id: number): boolean;
        get(id: number): any[];
    }

    // 实现泛型接口的类也应该是一个泛型类
    class MysqlDb<T> implements DBI<T> {
        constructor(){
            // build connection
        }
        add(info: T): boolean {
            console.log(info);            
            return true;
        }
        update(info: T, id: number): boolean {
            throw new Error("Method not implemented.");
        }
        delete(id: number): boolean {
            throw new Error("Method not implemented.");
        }
        get(id: number): any[] {
            throw new Error("Method not implemented.");
        }
    }
    class MongoDb<T> implements DBI<T> {
        constructor(){
            // build connection
        }

        add(info: T): boolean {
            console.log(info);            
            return true;
        }
        update(info: T, id: number): boolean {
            throw new Error("Method not implemented.");
        }
        delete(id: number): boolean {
            throw new Error("Method not implemented.");
        }
        get(id: number): any[] {
            throw new Error("Method not implemented.");
        }
    }

    class User {
        username: string | undefined;
        password: string | undefined;
    }
    class Article {
        title: string | undefined;
        desc: string | undefined;
    }
    let u=new User();
    u.username='张小三'
    u.password='888'
    let udb=new MysqlDb<User>();       // 实例化时使用类类型对add数据进行约束
    console.log(udb.add(u));

    let a=new Article();
    a.title='标题'
    a.desc='内容'
    let adb=new MysqlDb<Article>();    // 实例化时使用类类型对add数据进行约束
    console.log(adb.add(a));
















}


